namespace Domotica.Windows.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;

    public class SignAdorner : Adorner
    {
        private UIElement _element;
        private SignExtender _signExtender;
        [ThreadStatic]
        private static Brush brush = null;
        [ThreadStatic]
        private static Pen pen = null;

        public SignAdorner(SignExtender signExtender, UIElement element) : base(element)
        {
            this._signExtender = signExtender;
        }

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);
            this._signExtender.DoPreviewMouseUp(e);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this._signExtender.StylusPoints != null)
            {
                if (brush == null)
                {
                    brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20.0);
                    pen = new Pen(brush, 2.0);
                }
                Point prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
                foreach (Point pt in this._signExtender.StylusPoints)
                {
                    if (Point.Subtract(prevPoint, pt).Length > 4.0)
                    {
                        drawingContext.DrawLine(pen, prevPoint, pt);
                        prevPoint = pt;
                    }
                }
            }
        }
    }
}

